// *********************************************
// * Raamatukogu  *
// *********************************************
// sv_player.lua - Loads serverside player functionality

local metaPly = _R.Player

function metaPly:SetSpeed( walk, run, crouch, nosave )
	run = run or walk
	self:SetRunSpeed( run )
	self:SetWalkSpeed( walk )
	self:SetCrouchedWalkSpeed( crouch )
	self:SetMaxSpeed( math.max( run, walk, crouch ) )
	if (nosave) then return end
	self.WalkSpeed = walk
	self.RunSpeed = run
	self.CrouchSpeed = crouch
end

local colMe = Color( 100, 100, 200 )
function metaPly:ChatToSelf( Text, StartTime, DelayTime, snd, Volume )
	timer.Simple( StartTime or 0, function()
		if (type( Text ) != "table") then Text = { Text } end
		for i=1, #Text do
			timer.Simple( (i-1)*DelayTime, function()
				self:ChatMessage( colMe, "You", Text[i] )
			end )
		end
		if (snd) then
			self:EmitSound( snd, Volume )
		end
		return true
	end )
end

function metaPly:ChatMessage( col, txt1, txt2 )
	umsg.Start( "chat_msg", self )
		umsg.Vector( Vector( col.r, col.g, col.b ) )
		umsg.String( txt1 )
		umsg.String( txt2 )
	umsg.End()
end

function metaPly:Blind( bool )
	umsg.Start( "Blind", ply )
		umsg.Bool( bool )
	umsg.End()
end

function metaPly:SetCamera( name, parent )
	local ent = ents.FindByName( name )[1]
	if (!ValidEntity( ent )) then return ErrorNoHalt( "Failed to locate camera by name '" .. name .. "'!\n" ) end
	// print( "Setting active camera to '" .. name .. "'..." )
	ent:SetActive( self, parent )
	self.ActiveCamera = ent
end

function metaPly:GetCamera()
	return self.ActiveCamera
end

function metaPly:FadeCameras( name1, name2, time )
	local ent1 = ents.FindByName( name1 )[1]
	if (!ValidEntity( ent1 )) then return ErrorNoHalt( "Failed to locate camera by name '" .. name1 .. "'!\n" ) end
	local ent2 = ents.FindByName( name2 )[1]
	if (!ValidEntity( ent2 )) then return ErrorNoHalt( "Failed to locate camera by name '" .. name2 .. "'!\n" ) end
	umsg.Start( "_camerafade", self )
		umsg.Vector( ent1:GetPos() )
		umsg.Angle( ent1:GetAngles() )
		umsg.Vector( ent2:GetPos() )
		umsg.Angle( ent2:GetAngles() )
		umsg.Float( time )
	umsg.End()
end

function metaPly:SitFor( howlong )
	self:PlaySequence( "Sit_Ground", howlong+0.1 )
	timer.Simple( howlong, self.PlaySequence, self, "Sit_Ground_to_Idle", 2.18 )
	timer.Simple( howlong + 2.25, self.Freeze, self, false )
	self:Freeze( true )
end

function metaPly:FreezeNoWeps( b )
	self:Freeze( b )
	self.Frozen = b
end

function metaPly:FreezeFix( Start, End )
	timer.Simple(Start or 0, function()
		self:Lock()
		self:Freeze( true )
	end)
	timer.Simple(End or 0, function()
		self:UnLock()
		self:Freeze( false )
	end)
end

function metaPly:LayDownFor( howlong )
	self:PlaySequence( "laycouch1", howlong+0.1 )
	timer.Simple( howlong, self.PlaySequence, self, "laycouch1_exit", 2.18 )
end

function metaPly:PlayAnimationFor( howlong, startanimation, stopanimation, Freeze )
	if (startanimation) then
		self:PlaySequence( startanimation, howlong+0.1 )
	end
	if (stopanimation) then
		timer.Simple( howlong, self.PlaySequence, self, stopanimation, 2.18 )
	end
	if (Freeze) then
		timer.Simple( howlong + 2.25, self.Freeze, self, false )
		self:Freeze( true )
	end
end

function metaPly:DefenceMode( b )
	umsg.Start( "defencemode", self )
	umsg.Bool( b )
	umsg.End()
end

function metaPly:NoMove( b )
	if (b) then
		self:SetSpeed( 0, 0, 0, true )
	else
		self:SetSpeed( self.WalkSpeed, self.RunSpeed, self.CrouchSpeed, true )
	end
end

function metaPly:UseKey( doorname )
	for _, item in pairs( self.Inventory.Items ) do
		if (item.IsKey && (item.DoorName == doorname)) then
			self.Inventory:RemoveItem( item )
			item:Destroy()
			return true
		end
	end
end

function GM:PlayerInitialSpawn( ply )
	
end

function GM:PlayerSpawn( ply )
	if (ply.ActiveStory) then
		ply.ActiveStory:PlayerSpawn( ply )
		ply.ActiveStory:PlayerLoadout( ply )
		ply.ActiveStory:PlayerSetModel( ply )
	else
		GAMEMODE:PlayerSpawnAsSpectator( ply )
		local ent = ents.FindByClass( "lua_camera" )[1]
		if (ValidEntity( ent )) then ent:SetActive( ply ) end
		umsg.Start( "selectstory", ply )
		umsg.End()
	end
end

function GM:PlayerSelectSpawn( ply )
	if (ply.ActiveStory) then
		return ply.ActiveStory:PlayerSelectSpawn( ply )
	else
		return ply
	end
end

function GM:PlayerDeath( ply )
	local s = ply.ActiveStory
	if (!s) then return end
	timer.Simple( 2, s.Finish, s, ply )
	ply:ChatToSelf( "You died!" )
end

local Doors = {
	prop_door_rotating = true
}
local Props = {
	prop_physics = true
}

local function PlayerUse( ply, ent )
	ply.NextUse = ply.NextUse or 0
	if (CurTime() < ply.NextUse) then return false end
	ply.NextUse = CurTime() + 0.5
	local class = ent:GetClass()
	if (Doors[ class ]) then
		if (ply.ActiveStory) then
			return ply.ActiveStory:HandleEvent( ply, "usedoor", ent )
		end
	end
	if (Props[ class ]) then
		if (ply.ActiveStory) then
			return ply.ActiveStory:HandleEvent( ply, "useprop", ent )
		end
	end
end
hook.Add( "PlayerUse", "PlayerUse", PlayerUse )

function GM:PlayerSwitchFlashlight( ply, on )
	if (ply.ActiveStory) then
		return ply.ActiveStory:PlayerSwitchFlashlight( ply, on )
	else
		return not on
	end
end

function GM:ShowSpare1( ply )
	umsg.Start( "show_inv", ply )
	umsg.End()
end

local function cmdSelectStory( ply, com, args )
	if (!args[1]) then return end
	local s = story.Get( args[1] )
	if (!s) then return end
	ply.ActiveStory = s
	s:LoadChapter( s:GetChapterID() )
	timer.Simple( 1, function()
		s:Begin( ply )
		ply:KillSilent()
		ply:Spawn()
	end )
end
concommand.Add( "select_story", cmdSelectStory )

/*local function cmdSaveStory( ply, com, args )
	if (!ply.ActiveStory) then return end
	story.SaveState( ply, ply.ActiveStory )
end
concommand.Add( "save_story", cmdSaveStory )

local function cmdLoadStory( ply, com, args )
	if (!ply.ActiveStory) then return end
	story.LoadState( ply, ply.ActiveStory )
end
concommand.Add( "load_story", cmdLoadStory )*/